//
//  ScenicSearchViewController.m
//  TravelToHelp
//
//  Created by lanou3g on 15/10/31.
//  Copyright © 2015年 冯贵宾. All rights reserved.
//

#import "ScenicSearchViewController.h"
#import "FLSAttractionsQuery.h"
#import "ScenicSearchModel.h"
#import "ScenicSearchDetailViewController.h"

@interface ScenicSearchViewController ()<UISearchBarDelegate>

@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) FLSAttractionsQuery *attractionQuery;
@property (nonatomic, strong) ScenicSearchModel *model;
@property (nonatomic, assign) BOOL isClick;
@property (nonatomic, strong) AppDelegate *app;
@property (nonatomic, strong) DDIndicator *indicator;

@property (nonatomic, strong) UIView *viewV;

@end

@implementation ScenicSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
        _app = [[AppDelegate alloc] init];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self addSubViews];
    [self setNavigationBar];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.isClick = NO;
}

- (void)setNavigationBar
{
    // 创建搜索按钮
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, -1, 240, 30)];
    self.searchBar.barStyle = UIBarStyleDefault;
    self.searchBar.delegate = self;
    self.searchBar .placeholder = @"请输入景点";
    [self.searchBar becomeFirstResponder];
    self.searchBar.tintColor = [UIColor whiteColor];
    self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
    // 创建一个View 给titleView赋值
    UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 240, 28)];
    // 把搜索框添加到view上
    [tempView addSubview:self.searchBar];
    tempView.layer.cornerRadius = 14;
    tempView.layer.masksToBounds = YES;
    self.navigationItem.titleView = tempView;
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:(UIBarButtonItemStylePlain) target:self action:@selector(actionRightButton:)];
    self.navigationItem.rightBarButtonItem = rightButton;
    
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:(UIBarButtonItemStylePlain) target:self action:nil];
    self.navigationItem.leftBarButtonItem = leftButton;
    
}

- (void)addSubViews
{
    
    UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, kScreenWidth, 30)];
    titleLable.text = @"热点搜索";
    titleLable.textColor = [UIColor grayColor];
    titleLable.font = [UIFont systemFontOfSize:20];
    [self.view addSubview:titleLable];
    
    UIView *divisionView = [[UIView alloc] initWithFrame:CGRectMake(0, titleLable.bottom + 5, kScreenWidth, 1)];
    divisionView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:divisionView];
    
    
    NSMutableArray *nameArray = [NSMutableArray arrayWithObjects:@"颐和园", @"天坛", @"香山", @"故宫", @"鸟巢", @"水立方", nil];
    for (int i = 0; i < nameArray.count; i++) {
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        if (i < 3) {
            button.frame = CGRectMake(7.5 + (kScreenWidth - 10) / 3 * i, divisionView.bottom + 10, (kScreenWidth - 10) / 3 - 5, 50);
        } else {
            button.frame = CGRectMake(7.5 + (kScreenWidth - 10) / 3 * (i - 3), divisionView.bottom + 90, (kScreenWidth - 10) / 3 - 5, 50);
        }
        button.layer.borderWidth = 0.5;
        button.layer.cornerRadius = 25;
        button.layer.borderColor = [UIColor purpleColor].CGColor;
        [button setTitle:nameArray[i] forState:(UIControlStateNormal)];
        [button setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
        [button addTarget:self action:@selector(actionButton:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.view addSubview:button];
    }
}

#pragma mark -- navigationBar的点击方法
- (void)actionRightButton:(UIBarButtonItem *)rightButton
{
    [self.navigationController popViewControllerAnimated:NO];
}

#pragma mark --实现点击方法
- (void)actionButton:(UIButton *)button
{

    NSString *text = [button titleForState:normal];

    [self.searchBar resignFirstResponder];
    self.searchBar.text = @"";
    ScenicSearchDetailViewController *ssdVC = [[ScenicSearchDetailViewController alloc] init];
    ssdVC.placeText = text;
    [self.navigationController pushViewController:ssdVC animated:YES];
                    

}

#pragma mark -- 实现搜索框的代理方法
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    // 显示 取消按钮

    return YES;
    
}// return NO to not become first responder
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
   // NSLog(@"我已经开始编辑了");
}// called when text starts editing
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
   // NSLog(@"编辑应该结束了");
    return YES;
}
// return NO to not resign first responder
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
   // NSLog(@"编辑已经结束了");
}// called when text ends editing
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
  //  NSLog(@"文本框变化了");
}// called when text changes (including clear)
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text NS_AVAILABLE_IOS(3_0)
{
   // NSLog(@"应该改变文本矿的编辑  | 该清除搜索框");
    return YES;
}// called before text changes

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
   
    if ([_app internetReachableOrNot]) {
        [self addAnimation];
    [self.attractionQuery AttractionsQueryWithPlace:searchBar.text Block:^(ScenicSearchModel *model) {
         [self stopAnimation];
        if (model.state == YES) {
           
            [self.searchBar resignFirstResponder];
            self.searchBar.text = @"";
            ScenicSearchDetailViewController *ssdVC = [[ScenicSearchDetailViewController alloc] init];
            ssdVC.model = model;
            
            [self.navigationController pushViewController:ssdVC animated:YES];
        } else {
           
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Sorry" message:@"抱歉了亲,你搜索的景点暂未收录" preferredStyle:(UIAlertControllerStyleAlert)];
           
            // 创建按钮
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                 searchBar.text = @"";
                [searchBar resignFirstResponder];
            }];
            [alert addAction:okAction];
        [self presentViewController:alert animated:YES completion:nil];
        }
    }];
    } else {
        searchBar.text = @"";
        [searchBar resignFirstResponder];
        [self alertController];
    }
}

- (void)alertController
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"您的当前网络中断或网络信号差, 请检查网络后重新进入!!" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];
}


- (FLSAttractionsQuery *)attractionQuery
{
    if (!_attractionQuery) {
        _attractionQuery = [[FLSAttractionsQuery alloc] init];
    }
    return _attractionQuery;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
// 加载动画
- (void)addAnimation
{
    _viewV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    _viewV.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0];
    _viewV.layer.cornerRadius = 10;
    _viewV.layer.masksToBounds = YES;
    [self.view addSubview:_viewV];
    self.indicator = [[DDIndicator alloc] initWithFrame:CGRectMake((_viewV.width - 40) / 2, (_viewV.height - 40) / 2 - 100 , 40, 40)];
    [_viewV addSubview:self.indicator];
    [self.indicator startAnimating];
    // [self performSelector:@selector(stopLoading:) withObject:self.indicator afterDelay:5];
}

- (void)stopAnimation
{
    [self.indicator stopAnimating];
    [_viewV removeFromSuperview];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
